//
//  AzkarDeatailCell.m
//  Azkar
//
//  Created by 王易 on 2018/8/24.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "AzkarDeatailCell.h"

/**
 祈福详情Cell
 */
@interface AzkarDeatailCell()
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UIImageView *hotImage;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *message;
@property (weak, nonatomic) IBOutlet MyLinearLayout *contentLayout;

/**
 祈祷
 */
@property (weak, nonatomic) IBOutlet UILabel *prayer;

/**
 分享
 */
@property (weak, nonatomic) IBOutlet UILabel *share;

@property (weak, nonatomic) IBOutlet MyLinearLayout *shareLayout;
@property (weak, nonatomic) IBOutlet MyLinearLayout *messageLayout;

/**
 评论数量
 */
@property (weak, nonatomic) IBOutlet UILabel *messageCount;


@property (weak, nonatomic) IBOutlet UIImageView *prayerImage;
@property (weak, nonatomic) IBOutlet MyRelativeLayout *prayerLayout;
@property (weak, nonatomic) IBOutlet MyRelativeLayout *relativeLayout;

@end
@implementation AzkarDeatailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
//    [MyBaseLayout setIsRTL:YES];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentLayout.myLeading = self.contentLayout.myTrailing = 0;
    self.contentLayout.wrapContentHeight = YES;
    self.contentLayout.cacheEstimatedRect = YES;
    
    self.rootLayout = self.contentLayout;
    
    self.relativeLayout.layer.cornerRadius = 3;
    self.relativeLayout.layer.masksToBounds = YES;
    
    self.icon.layer.cornerRadius = 17;
    self.icon.layer.masksToBounds = YES;

    self.userName.leadingPos.equalTo(self.icon.trailingPos).offset(8);
    self.userName.trailingPos.equalTo(self.hotImage.leadingPos).offset(8);
    
    self.time.leadingPos.equalTo(self.userName.leadingPos);
    self.time.topPos.equalTo(self.userName.bottomPos).offset(3);

    
    self.message.topPos.equalTo(self.time.bottomPos).offset(14.5);
  
    
    self.prayerLayout.layer.cornerRadius = 13;
    self.prayerLayout.layer.masksToBounds = YES;
    self.prayerLayout.topPos.equalTo(self.message.bottomPos).offset(24);
    
    self.prayer.leadingPos.equalTo(self.prayerImage.trailingPos).offset(5.5);
 
    
    self.messageLayout.orientation = MyOrientation_Horz;
    self.messageLayout.topPos.equalTo(self.prayerLayout.topPos);
    
    self.shareLayout.orientation = MyOrientation_Horz;
    self.shareLayout.topPos.equalTo(self.prayerLayout.topPos);
    self.shareLayout.trailingPos.equalTo(self.messageLayout.leadingPos).offset(30);

    [self.prayerLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
        [self likeData];
    }];
    WeakSelf
    [self.shareLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
        StrongSelf
//        if (![AppDataManager sharedManager].isLogin) {
//            UINavigationController *nav = [UINavigationController currentNC];
//            Class loginCtrClass = NSClassFromString(@"LoginController");
//            if (loginCtrClass) {
//                UIViewController *ctr = [loginCtrClass new];
//                ctr.hidesBottomBarWhenPushed = YES;
//                [nav pushViewController:ctr animated:YES];
//            }
//            return ;
//        }
        NSString *lang_type = self.blessModel.lang_type;

        ShareView *shareView = [ShareView new];
        shareView.lang_type = lang_type;
        shareView.isPublic = NO;
        shareView.Id = self.blessModel.ID;
        shareView.isReport = NO;
        [shareView showViewWith:self.blessModel];
        shareView.block = ^{
            self.blessModel.cnt_shared = [NSString stringWithFormat:@"%ld",self.blessModel.cnt_shared.integerValue+1];
            [self setBlessModel:self.blessModel];
        };
        
        NSDictionary *dic;
        if (self.isTop) {
            dic = @{@"top_shareduasid":self.blessModel.ID,@"top_shareduas":self.blessModel.cnt_shared};
        }
        else{
            dic = @{@"new_shareduasid":self.blessModel.ID,@"new_shareduas":self.blessModel.cnt_shared};
        }
        [FIRAnalytics logEventWithName:@"a_click_shareduas"
                            parameters:dic];
        
    }];
    
}

- (void)likeData
{
    NSString *url;
    
    BOOL like;
    
    if (!self.blessModel.ID) {
        [AlertTools showMessage:TLString(@"error")];
        return;
    }
    url = [NSString stringWithFormat:@"%@/%@/like",BlessBlog,self.blessModel.ID];
    like = self.blessModel.like;
    NSDictionary *dic;
    if (self.isTop) {
        dic = @{@"top_duaid":_blessModel.ID,@"top_duasforthis":_blessModel.cnt_like};
    }
    else{
        dic = @{@"new_duaid":_blessModel.ID,@"new_duasforthis":_blessModel.cnt_like};
    }
    [FIRAnalytics logEventWithName:@"a_click_duasforthis"
                        parameters:dic];
    [AlertTools showLoading];
    if(!like){
        
        [[AzkarApi postData:url withParm:nil]subscribeNext:^(id x) {
            [AlertTools hideLoading];
            self.blessModel.like = YES;
            self.blessModel.cnt_like = [NSString stringWithFormat:@"%@",x];
            self.blessModel = self.blessModel;
           
        } error:^(NSError *error) {
//            [AlertTools hideLoading];
        }];
    }else{
        
        [[AzkarApi deleteData:url withParm:nil]subscribeNext:^(id x) {
            [AlertTools hideLoading];
            self.blessModel.like = NO;
            self.blessModel.cnt_like = [NSString stringWithFormat:@"%@",x];
            self.blessModel = self.blessModel;
     
        } error:^(NSError *error) {
            [AlertTools hideLoading];
        }];
        
    }
}
- (void)setBlessModel:(BlessModel *)blessModel
{
    _blessModel = blessModel;
    
    self.userName.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    self.time.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    self.message.numberOfLines = 2;
    self.message.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
   self.prayer.textAlignment = [MyBaseLayout isRTL] ? NSTextAlignmentRight : NSTextAlignmentLeft;
    
    
    self.share.text = blessModel.cnt_shared;
    [self.share sizeToFit];
    
    self.messageCount.text = blessModel.cnt_comment;
    [self.messageCount sizeToFit];

    
    if (blessModel.like) {
        self.prayer.text = [NSString stringWithFormat:@"%@ %@",blessModel.cnt_like,TLString(@"greeting_cards_azkar_Ameen_ya_rab")];
        self.prayer.textColor = Color(@"#929292");
        self.prayerLayout.backgroundColor = Color(@"#EFEFEF");
        self.prayerImage.image = Image(@"dua_ic_s");
    }else{
        
        
        self.prayer.textColor = Color(@"#FFFFFF");
        self.prayerLayout.backgroundColor = Color(@"#2B9958");
        self.prayer.text = [NSString stringWithFormat:@"%@ %@",blessModel.cnt_like,TLString(@"greeting_cards_azkar_Duas_for_this")];
        self.prayerImage.image = Image(@"dua_ic");
    }
    self.message.text = blessModel.content;
    if (blessModel.is_anonymous) {
        self.icon.image = ICONIMGE;
        self.userName.text = TLString(@"greeting_cards_Anonymously");
    }else{
        [self.icon sd_setImageWithURL:[NSURL URLWithString:blessModel.avatar] placeholderImage:ICONIMGE];
        
        self.userName.text = blessModel.uname;
        
    }
    

    self.time.text = [DateHelper dateStrFromTimeStamp:blessModel.create_date dateFormat:@"dd/MM HH:mm"];
    if ([[LanguageManager defaultManager].appLanguage isEqualToString:@"ar"]) {
        self.time.text = self.time.text.arabicString;
        self.prayer.text = self.prayer.text.arabicString;
        self.share.text = self.share.text.arabicString;
        self.messageCount.text = self.messageCount.text.arabicString;
    }
    
    
    self.hotImage.myVisibility = [blessModel.cnt_like integerValue] > 100 ? MyVisibility_Visible : MyVisibility_Gone;
    self.hotImage.image =  [MyBaseLayout isRTL] ? [UIImage RTL_ImageWithName:@"hot_label"] :  Image(@"hot_label");
    
    
    [self.time sizeToFit];
    [self.userName sizeToFit];
    [self.message sizeToFit];
    [self.prayer sizeToFit];
}
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority
{
    /*
     通过布局视图的sizeThatFits方法能够评估出UITableViewCell的动态高度。sizeThatFits并不会进行布局而只是评估布局的尺寸。
     因为cell的高度是自适应的，因此这里通过调用高度为wrap的布局视图的sizeThatFits来获取真实的高度。
     */
    return [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width, targetSize.height)];  //如果使用系统自带的分割线，请记得将返回的高度height+1
    
}

@end
